package com.utils.movieClipToBitmap
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	/**
	 * 将加载的swf转换成BitmapData序列 
	 * @author Admin
	 */	
	public class MovieClipToBitmapData extends Sprite
	{
		public static const instance:MovieClipToBitmapData = new MovieClipToBitmapData();
		private static const PREFIX_SWF_TAG:String = 'assets/panels/';
		private static const SUFFIX_SWF_TAG:String = '.swf';
		private var _resourceBox:Object = new Object();//资源池
		private var _curFileName:String = '';//当期文件名
		private var _swfList:Array = [];//swf名称数组
		private var _loader:Loader;
		private var _loaderContent:MovieClip;//mc容器
		private var _curMovieClip:MovieClip;//当前的mc
		private var _bmd:BitmapData;//位图数据
		private var _curFrame:int = 1;//当前帧数
		private var _curSenceWidth:Number;
		private var _curSenceHeight:Number;
		
		private var _switchOver:Function = new Function();
		
		public function MovieClipToBitmapData()
		{
			if(instance) throw Error('不能重复实例化!');
		}
		
		/**
		 * 添加要加载的swf文件
		 * @param params swf文件名数组
		 */		
		public function addAssets(...params):void
		{
			_swfList = params;
			if(!_loader)  _loader = new Loader();
			if(!_loader.contentLoaderInfo.hasEventListener(Event.COMPLETE)){
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
			}
			if(!_loader.contentLoaderInfo.hasEventListener(IOErrorEvent.IO_ERROR)){
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIoErrorHandler);
			}
			loopLoad(_swfList.pop());
		}
		
		/**
		 *  循环执行加载
		 * @param fileName
		 */		
		private function loopLoad(fileName:String):void
		{
			_curFileName = fileName;
			_loader.load(new URLRequest(PREFIX_SWF_TAG + fileName + SUFFIX_SWF_TAG));
		}
		
		private function onIoErrorHandler(e:IOErrorEvent):void
		{
			trace('IOErrorEvent:' + e.text);
		}
		
		/**加载完执行函数*/		
		private function onCompleteHandler(e:Event):void
		{
			_loaderContent = _loader.content as MovieClip;
			dataSwitch(_loaderContent,_curFileName);
			if(_swfList.length > 0){
				loopLoad(_swfList.pop());
			}else{
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onCompleteHandler);
				_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onIoErrorHandler);
				_loader.unload();
				_loader = null;
				trace('////////////清理加载器!////////////');
				loadOverDoFunction();
			}	
		}
		
		/**
		 * 全部资源加载完成后执行
		 * @param 使用前先设置回调方法
		 */		
		public function loadOverDoFunction():void
		{
			BitmapClip.resourceBox = _resourceBox;//将资源赋值给图片剪辑
			_switchOver();
		}
		
		/**
		 * 数据转换
		 * @param target   loader.content 或是 目标mc
		 * @param curFileName   当前处理文件名,做为资源池的键使用
		 * @param overDo   当前文件处理完成后是否执行回调函数
		 */		
		public function dataSwitch(target:MovieClip,curFileName:String,overDo:Boolean = false):void
		{
			_curFileName = curFileName;
			_curSenceWidth = target.width;
			_curSenceHeight= target.height;
			if(target.totalFrames == 1){
				_curMovieClip = MovieClip(target.getChildAt(0));
				if(!_curMovieClip) _curMovieClip= target;
			}else{
				_curMovieClip = target;
			}
			trace('影片剪辑帧数:' + _curMovieClip.totalFrames);
			_curMovieClip.stop();
			_resourceBox[_curFileName] = [];
			_curFrame = 1;//初始化当前帧数
			getBitmapData();//处理数据完毕后在继续加载
			if(overDo) loadOverDoFunction();
		}
		
		/**获取BitmapData数据*/		
		private function getBitmapData():void
		{
			_curMovieClip.gotoAndStop(_curFrame);
			_bmd = new BitmapData(_curSenceWidth,_curSenceHeight,true,0);
			_bmd.draw(_curMovieClip);//获取副本
			_resourceBox[_curFileName].push(_bmd);//存入资源池
			//trace('当前帧数:' + _curFrame+'/'+_curMovieClip.totalFrames);
			if(_curFrame < _curMovieClip.totalFrames){
				++_curFrame;//手动跳帧
				getBitmapData();
			}else{
				_curMovieClip.stop();//销毁mc
				_curMovieClip = null;
			}
		}
		
		/**swf加载并处理完毕回调函数*/		
		public function set switchOver(value:Function):void{_switchOver = value;}
		
		
		
		
		
		
		
		
		
	}
}